# coding: utf-8
import sys
import json

sys.path.append("..")
from raft.node_launcher import StaticNodeLauncher

FAKENODE = {
    # [MAC, port, client_port]
    '01': ['FAKENODE__01', 10001, 9999],
    '02': ['FAKENODE__02', 10002, 9998],
    '03': ['FAKENODE__03', 10003, 9997],
    '04': ['FAKENODE__04', 10004, 9996]
}

if __name__ == '__main__':
    # 构造伪造的地址，来在单机上模拟多机运行环境
    print(sys.argv)
    node_id = sys.argv[1]
    conf = {
        'id': FAKENODE[node_id][0],
        'addr': ('localhost', FAKENODE[node_id][1]),
        'client_port': FAKENODE[node_id][2],
        'peers': dict(),
        'heartbeat_interval_ms': (1000, 2000),
        'wait_ms': (6000, 15000)
    }

    for n in FAKENODE:
        if n != node_id:
            conf['peers'][FAKENODE[n][0]] = ('localhost', FAKENODE[n][1])
    conf['static_log_name'] = 'key{}.json'.format(node_id)

    # 构造launcher对象
    print(conf)
    launcher = StaticNodeLauncher(conf)

    # 开始node的工作
    launcher.launch()
